


using System.Numerics;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_Lerp_RT : RT_Node
    {
        public RT_SingleValuePort From_RT;

        public RT_SingleValuePort To_RT;

        public RT_SingleValuePort Time_RT;

        public RT_SingleValuePort Clamp_RT;

        public RT_SingleValuePort Result_RT;

        public Node_Lerp_RT(Node_Lerp sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            From_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.From, this);

            To_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.To, this);

            Time_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Time, this);

            Clamp_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Clamp, this);

            Result_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValue = GetResult;
        }

        public AValue GetResult(DAG_Executor executor)
        {
            Result_RT.SourceValue.Reset();
            float time = Time_RT.GetValue(executor).ToScalar().Item2;
            bool clamp = Clamp_RT.GetValue(executor).ToBoolean().Item2;

            switch (Result_RT.SourceValue.ValueType)
            {
                case AValue.EValueType.Unknown: break;
                case AValue.EValueType.Boolean: break;
                case AValue.EValueType.Integer: break;
                case AValue.EValueType.Scalar:
                    float fFrom = From_RT.GetValue(executor).ToScalar().Item2;
                    float fTo = To_RT.GetValue(executor).ToScalar().Item2;
                    float fResult = XDFLib.XMath.Lerp(fFrom, fTo, time, clamp);
                    Result_RT.SourceValue.SetScalar(fResult);
                    break;
                case AValue.EValueType.Vector2:
                    Vector2 v2From = From_RT.GetValue(executor).ToVector2().Item2;
                    Vector2 v2To = To_RT.GetValue(executor).ToVector2().Item2;
                    if (clamp)
                    {
                        if (time <= 0)
                            Result_RT.SourceValue.SetVector2(v2From);
                        else if (time >= 1)
                            Result_RT.SourceValue.SetVector2(v2To);
                    }
                    else
                    {
                        Vector2 v2Result = Vector2.Lerp(v2From, v2To, time);
                        Result_RT.SourceValue.SetVector2(v2Result);
                    }
                    break;
                case AValue.EValueType.Vector3:
                    Vector3 v3From = From_RT.GetValue(executor).ToVector3().Item2;
                    Vector3 v3To = To_RT.GetValue(executor).ToVector3().Item2;
                    if (clamp)
                    {
                        if (time <= 0)
                            Result_RT.SourceValue.SetVector3(v3From);
                        else if (time >= 1)
                            Result_RT.SourceValue.SetVector3(v3To);
                    }
                    else
                    {
                        Vector3 v3Result = Vector3.Lerp(v3From, v3To, time);
                        Result_RT.SourceValue.SetVector3(v3Result);
                    }
                    break;
                case AValue.EValueType.String: break;
                case AValue.EValueType.Enum: break;
                default: break;
            }

            return Result_RT.SourceValue;
        }
    }
}